package net.skyaims.sqlmgr.resource;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import lombok.RequiredArgsConstructor;
import net.skyaims.sqlmgr.constant.ConstData;
import net.skyaims.sqlmgr.dao.HostDao;
import net.skyaims.sqlmgr.dao.IdcServerDao;
import net.skyaims.sqlmgr.domain.HostTable;
import net.skyaims.sqlmgr.domain.IdcServer;
import net.skyaims.sqlmgr.util.UUIDUtil;

@Produces(MediaType.APPLICATION_JSON)
@RequiredArgsConstructor
@Path("/sqlmgr/idcServer")
public class IdcServerResource {

	private final IdcServerDao idcServerDao;
	private final HostDao hostDao;

	@Path("/update/uuid")
	@GET
	public String updateIdcServer() {

		try {

			List<Integer> idcIds = new ArrayList<Integer>();
			List<Integer> nodeIds = new ArrayList<Integer>();

			List<IdcServer> servers = idcServerDao.getAll();
			List<HostTable> hosts = hostDao.getAll();

			for (IdcServer server : servers) {
				idcIds.add(server.getSid());
			}
			System.out.println("iplist : " + idcIds);

			for (HostTable host : hosts) {
				nodeIds.add(host.getHid());
			}
			System.out.println("node : " + nodeIds);

			idcIds.retainAll(nodeIds);
			System.out.println("in : " + idcIds);

			for (Integer id : idcIds) {
				String uuid = UUIDUtil.random();
				idcServerDao.updateUuidById(id, uuid);
				hostDao.updateUuidById(id, uuid);
			}

			return ConstData.SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ConstData.FAILD;
		}

	}

	public static void main(String[] args) {

		List<Integer> idcIds;
		List<Integer> nodeIds;

		idcIds = new ArrayList<Integer>(Arrays.asList(1, 2, 4, 5));
		nodeIds = new ArrayList<Integer>(Arrays.asList(1, 3, 5));
		idcIds.retainAll(nodeIds);

		System.out.println(idcIds);
		for (Integer i : idcIds) {
			System.out.println(i);
		}

	}
}
